package com.cqie.service.impl;

import com.cqie.dto.user.DoctorInfoDto;
import com.cqie.dto.TokenUserLoginDto;
import com.cqie.entity.DoctorInfo;
import com.cqie.exception.SpecialException;
import com.cqie.mapper.user.DoctorInfoMapper;
import com.cqie.service.IDoctorInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.threadlocal.UserThreadLocal;
import com.cqie.util.RedisUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 不吃海鲜
 * @since 2024-11-25
 */
@Service
@Slf4j
public class DoctorInfoServiceImpl extends ServiceImpl<DoctorInfoMapper, DoctorInfo> implements IDoctorInfoService {

    @Resource
    private DoctorInfoMapper doctorInfoMapper;
    private final RedisUtils redisUtils;


    public DoctorInfoServiceImpl(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 获取医生基本信息
     * @return
     */
    @Override
    public DoctorInfoDto getDoctorInfo() {

        // 获取登陆账号信息
        TokenUserLoginDto tokenDto = UserThreadLocal.get(TokenUserLoginDto.class);

        var doctorInfo = doctorInfoMapper.getDoctorInfo(tokenDto.getDoctorId());
        if (ObjectUtils.isEmpty(doctorInfo)) {
            return new DoctorInfoDto();
        }

        DoctorInfoDto doctorInfoDto = new DoctorInfoDto();
        // 使用 BeanUtils.copyProperties 复制属性
        BeanUtils.copyProperties(doctorInfo, doctorInfoDto);

        return doctorInfoDto;
    }

    /**
     * 保存 / 更新 医生基本信息
     * @param doctorInfoDto
     * @return
     */
    @Override
    public Boolean saveDoctorInfo(DoctorInfoDto doctorInfoDto) {
        // 获取登陆账号信息
        TokenUserLoginDto tokenDto = UserThreadLocal.get(TokenUserLoginDto.class);

        // 判断是否为更新
        var isDoctorInfo = doctorInfoMapper.getDoctorInfo(tokenDto.getDoctorId());


        DoctorInfo doctorInfo = DoctorInfo.builder()
                .doctorId(tokenDto.getDoctorId())
                .departmentCode(doctorInfoDto.getDepartmentCode())
                .departmentName(doctorInfoDto.getDepartmentName())
                .educationCode(doctorInfoDto.getEducationCode())
                .educationName(doctorInfoDto.getEducationName())
                .doctorLevelCode(doctorInfoDto.getDoctorLevelCode())
                .doctorLevelName(doctorInfoDto.getDoctorLevelName())
                .doctorEmail(doctorInfoDto.getDoctorEmail())
                .doctorName(doctorInfoDto.getDoctorName())
                .doctorIdCard(doctorInfoDto.getDoctorIdCard())
                .doctorSexCode(doctorInfoDto.getDoctorSexCode())
                .doctorSexName(doctorInfoDto.getDoctorSexName())
                .build();

        // 新增
        if (ObjectUtils.isEmpty(isDoctorInfo))
        {
            doctorInfoMapper.insert(doctorInfo);
        }
        // 更新
        else{
            doctorInfo.setDoctorInfoId(isDoctorInfo.getDoctorInfoId());
            doctorInfoMapper.updateDoctorInfoByDoctorId(doctorInfo);
        }

        return true;
    }

    /**
     * 根据医生id获取医生信息
     * @param doctorId
     * @return
     */
    @Override
    public DoctorInfo getDoctorInfoById(Integer doctorId) {
        return doctorInfoMapper.selectByDoctorId(doctorId);
    }


    /**
     * 查询科室下的医生信息
     * @param departmentCode
     * @return
     */
    @Override
    public List<DoctorInfoDto> queryDoctorInfoUnderDepartment(String departmentCode) {
        return doctorInfoMapper.queryDoctorInfoUnderDepartment(departmentCode);
    }


}
